<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态文字弹幕</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            font-family: 'Arial', sans-serif;
            overflow: hidden;
            height: 100vh;
        }

        .danmaku-container {
            position: relative;
            width: 100vw;
            height: 100vh;
            overflow: hidden;
            background: rgba(0, 0, 0, 0.1);
        }

        .danmaku-item {
            position: absolute;
            white-space: nowrap;
            font-size: 18px;
            font-weight: bold;
            color: white;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
            z-index: 10;
            pointer-events: none;
            animation: moveLeft linear forwards;
        }

        /* 不同颜色的弹幕 */
        .danmaku-item.color-1 { color: #ff6b6b; }
        .danmaku-item.color-2 { color: #4ecdc4; }
        .danmaku-item.color-3 { color: #45b7d1; }
        .danmaku-item.color-4 { color: #96ceb4; }
        .danmaku-item.color-5 { color: #feca57; }
        .danmaku-item.color-6 { color: #ff9ff3; }
        .danmaku-item.color-7 { color: #54a0ff; }
        .danmaku-item.color-8 { color: #5f27cd; }

        /* 弹幕动画 */
        @keyframes moveLeft {
            from {
                transform: translateX(100vw);
            }
            to {
                transform: translateX(-100%);
            }
        }

        /* 控制面板样式 */
        .control-panel {
            position: fixed;
            top: 20px;
            left: 20px;
            background: rgba(255, 255, 255, 0.9);
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
            z-index: 100;
            min-width: 250px;
        }

        .control-panel h3 {
            margin-bottom: 15px;
            color: #333;
            text-align: center;
        }

        .input-group {
            margin-bottom: 15px;
        }

        .input-group label {
            display: block;
            margin-bottom: 5px;
            color: #555;
            font-weight: bold;
        }

        .input-group input, .input-group textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }

        .input-group textarea {
            height: 80px;
            resize: vertical;
        }

        .button-group {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        button {
            padding: 8px 16px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            font-weight: bold;
            transition: all 0.3s ease;
        }

        .btn-primary {
            background: #4CAF50;
            color: white;
        }

        .btn-primary:hover {
            background: #45a049;
            transform: translateY(-2px);
        }

        .btn-secondary {
            background: #2196F3;
            color: white;
        }

        .btn-secondary:hover {
            background: #1976D2;
            transform: translateY(-2px);
        }

        .btn-danger {
            background: #f44336;
            color: white;
        }

        .btn-danger:hover {
            background: #d32f2f;
            transform: translateY(-2px);
        }

        .btn-warning {
            background: #ff9800;
            color: white;
        }

        .btn-warning:hover {
            background: #f57c00;
            transform: translateY(-2px);
        }

        /* 预设弹幕按钮 */
        .preset-danmaku {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 8px;
            margin-top: 10px;
        }

        .preset-btn {
            padding: 6px 10px;
            font-size: 12px;
            background: #e0e0e0;
            color: #333;
        }

        .preset-btn:hover {
            background: #d0d0d0;
            transform: none;
        }

        /* 统计信息 */
        .stats {
            position: fixed;
            bottom: 20px;
            right: 20px;
            background: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 10px 15px;
            border-radius: 5px;
            font-size: 12px;
            z-index: 100;
        }
    </style>
</head>
<body>
    <div class="danmaku-container" id="danmakuContainer"></div>

    <!-- 控制面板 -->
    <div class="control-panel">
        <h3>🎮 弹幕控制台</h3>
        
        <div class="input-group">
            <label for="danmakuText">弹幕内容：</label>
            <textarea id="danmakuText" placeholder="输入要发送的弹幕内容..." rows="3">欢迎来到直播间！</textarea>
        </div>

        <div class="input-group">
            <label for="speedControl">速度 (秒)：</label>
            <input type="range" id="speedControl" min="3" max="15" value="8" step="1">
            <span id="speedValue">8秒</span>
        </div>

        <div class="input-group">
            <label for="fontSizeControl">字体大小：</label>
            <input type="range" id="fontSizeControl" min="14" max="32" value="18" step="2">
            <span id="fontSizeValue">18px</span>
        </div>

        <div class="button-group">
            <button class="btn-primary" onclick="sendDanmaku()">发送弹幕</button>
            <button class="btn-secondary" onclick="toggleAutoMode()">自动模式</button>
            <button class="btn-danger" onclick="clearAllDanmaku()">清空弹幕</button>
            <button class="btn-warning" onclick="pauseAutoMode()">暂停/继续</button>
        </div>

        <div class="preset-danmaku">
            <button class="preset-btn" onclick="sendPresetDanmaku('666666')">666</button>
            <button class="preset-btn" onclick="sendPresetDanmaku('太棒了！')">太棒了！</button>
            <button class="preset-btn" onclick="sendPresetDanmaku('主播加油！')">主播加油！</button>
            <button class="preset-btn" onclick="sendPresetDanmaku('哈哈哈')">哈哈哈</button>
            <button class="preset-btn" onclick="sendPresetDanmaku('支持主播')">支持主播</button>
            <button class="preset-btn" onclick="sendPresetDanmaku('再来一个')">再来一个</button>
        </div>
    </div>

    <!-- 统计信息 -->
    <div class="stats">
        <div>活跃弹幕: <span id="activeCount">0</span></div>
        <div>总发送数: <span id="totalCount">0</span></div>
    </div>

    <script>
        class DanmakuSystem {
            constructor() {
                this.container = document.getElementById('danmakuContainer');
                this.autoMode = false;
                this.autoInterval = null;
                this.isPaused = false;
                this.totalCount = 0;
                this.activeCount = 0;
                this.danmakuColors = ['color-1', 'color-2', 'color-3', 'color-4', 'color-5', 'color-6', 'color-7', 'color-8'];
                
                this.init();
            }

            init() {
                // 绑定速度控制
                const speedControl = document.getElementById('speedControl');
                const speedValue = document.getElementById('speedValue');
                speedControl.addEventListener('input', (e) => {
                    speedValue.textContent = e.target.value + '秒';
                });

                // 绑定字体大小控制
                const fontSizeControl = document.getElementById('fontSizeControl');
                const fontSizeValue = document.getElementById('fontSizeValue');
                fontSizeControl.addEventListener('input', (e) => {
                    fontSizeValue.textContent = e.target.value + 'px';
                });

                // 自动发送一些初始弹幕
                setTimeout(() => {
                    this.sendDanmaku('欢迎使用动态弹幕系统！');
                }, 1000);
                
                setTimeout(() => {
                    this.sendDanmaku('可以自定义颜色、速度和字体大小哦~');
                }, 3000);
            }

            createDanmaku(text, options = {}) {
                const danmaku = document.createElement('div');
                danmaku.className = 'danmaku-item';
                
                // 随机颜色
                const randomColor = this.danmakuColors[Math.floor(Math.random() * this.danmakuColors.length)];
                danmaku.classList.add(randomColor);
                
                // 设置文本内容
                danmaku.textContent = text;
                
                // 获取设置参数
                const speed = parseInt(document.getElementById('speedControl').value) || 8;
                const fontSize = parseInt(document.getElementById('fontSizeControl').value) || 18;
                
                // 应用样式
                danmaku.style.fontSize = fontSize + 'px';
                danmaku.style.top = Math.random() * (window.innerHeight - 100) + 'px';
                danmaku.style.animationDuration = speed + 's';
                
                // 添加到容器
                this.container.appendChild(danmaku);
                
                // 更新统计
                this.totalCount++;
                this.activeCount++;
                this.updateStats();
                
                // 动画结束后移除元素
                setTimeout(() => {
                    if (danmaku.parentNode) {
                        danmaku.parentNode.removeChild(danmaku);
                        this.activeCount--;
                        this.updateStats();
                    }
                }, speed * 1000);
                
                return danmaku;
            }

            sendDanmaku(text) {
                const danmakuText = text || document.getElementById('danmakuText').value.trim();
                if (!danmakuText) {
                    alert('请输入弹幕内容！');
                    return;
                }
                
                // 限制文本长度
                const displayText = danmakuText.length > 20 ? danmakuText.substring(0, 20) + '...' : danmakuText;
                this.createDanmaku(displayText);
                
                // 清空输入框
                if (text === undefined) {
                    document.getElementById('danmakuText').value = '';
                }
            }

            sendPresetDanmaku(text) {
                this.createDanmaku(text);
            }

            toggleAutoMode() {
                this.autoMode = !this.autoMode;
                
                if (this.autoMode) {
                    this.startAutoMode();
                    alert('自动模式已开启！每3-6秒自动发送弹幕');
                } else {
                    this.stopAutoMode();
                    alert('自动模式已关闭');
                }
            }

            startAutoMode() {
                this.stopAutoMode(); // 先清除之前的定时器
                
                const autoSend = () => {
                    if (!this.isPaused && this.autoMode) {
                        // 随机选择预设文本或默认文本
                        const presetTexts = [
                            '这是一条自动弹幕 🎉',
                            '弹幕飞过~ ✨',
                            '自动模式测试',
                            '动态效果很棒！',
                            '继续加油哦~',
                            '直播很精彩！',
                            '感谢分享！',
                            '内容很有趣'
                        ];
                        
                        const randomText = presetTexts[Math.floor(Math.random() * presetTexts.length)];
                        this.createDanmaku(randomText);
                    }
                    
                    // 随机间隔 3-6 秒
                    const nextInterval = 3000 + Math.random() * 3000;
                    this.autoInterval = setTimeout(autoSend, nextInterval);
                };
                
                autoSend();
            }

            stopAutoMode() {
                if (this.autoInterval) {
                    clearTimeout(this.autoInterval);
                    this.autoInterval = null;
                }
            }

            pauseAutoMode() {
                this.isPaused = !this.isPaused;
                const status = this.isPaused ? '已暂停' : '已继续';
                console.log('自动模式' + status);
            }

            clearAllDanmaku() {
                const danmakus = this.container.querySelectorAll('.danmaku-item');
                danmakus.forEach(danmaku => {
                    danmaku.style.opacity = '0';
                    danmaku.style.transform = 'scale(0.8)';
                    danmaku.style.transition = 'all 0.3s ease';
                    
                    setTimeout(() => {
                        if (danmaku.parentNode) {
                            danmaku.parentNode.removeChild(danmaku);
                        }
                    }, 300);
                });
                
                this.activeCount = 0;
                this.updateStats();
                
                setTimeout(() => {
                    alert('弹幕已清空！');
                }, 350);
            }

            updateStats() {
                document.getElementById('activeCount').textContent = this.activeCount;
                document.getElementById('totalCount').textContent = this.totalCount;
            }
        }

        // 初始化弹幕系统
        const danmakuSystem = new DanmakuSystem();

        // 全局函数（供HTML调用）
        function sendDanmaku() {
            danmakuSystem.sendDanmaku();
        }

        function sendPresetDanmaku(text) {
            danmakuSystem.sendPresetDanmaku(text);
        }

        function toggleAutoMode() {
            danmakuSystem.toggleAutoMode();
        }

        function clearAllDanmaku() {
            danmakuSystem.clearAllDanmaku();
        }

        function pauseAutoMode() {
            danmakuSystem.pauseAutoMode();
        }

        // 键盘快捷键
        document.addEventListener('keydown', (e) => {
            if (e.ctrlKey && e.key === 'Enter') {
                sendDanmaku();
            }
        });

        // 回车发送弹幕
        document.getElementById('danmakuText').addEventListener('keypress', (e) => {
            if (e.key === 'Enter' && !e.shiftKey) {
                e.preventDefault();
                sendDanmaku();
            }
        });
    </script>
</body>
</html>